package com.kitty.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kitty.dao.IUserDao;
import com.kitty.model.TUser;

public class UserDao extends HibernateDaoSupport implements IUserDao{

	public TUser get(String id) {
		return (TUser)getHibernateTemplate().get(TUser.class, id);
	}

	public void delete(String id) {
		TUser user = (TUser)getHibernateTemplate().get(TUser.class, id);
		if (user != null)
			getHibernateTemplate().delete(user);
	}

	public void delete(TUser user) {
		if (user != null)
			getHibernateTemplate().delete(user);
	}

	@SuppressWarnings("unchecked")
	public List<TUser> findAll() {
		return (List<TUser>)getHibernateTemplate().find("from TUser");
	}

	public String save(TUser user) {
		if (user == null)
			return null;
		
		if ("".equals(user.id))
			user.setId(null);
		
		getHibernateTemplate().saveOrUpdate(user);
		return user.id;
	}
	
	@SuppressWarnings("unchecked")
	public TUser findUserByEmail(String email){
		List<TUser> list = (List<TUser>)getHibernateTemplate().find
			(
				"from TUser where email = ?",
				new String[] {email}
			);
		
		if (list.size() > 0) {
			return list.get(0);
		}
		
		return null;
	}
}
